package com.rmondjone.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * 状态栏工具类
 */
public class StatusBarUtil {

    private static int sStatusBarHeight = 0;
    private static Context mContext;

    public static void init(Context context) {
        mContext = context.getApplicationContext();
    }

    /**
     * 获得状态栏高度.
     * 注意.
     * 如果是设置了全屏的activity可以直接获取高度, 对于这个activity里面的fragment,
     * 就需要通过{@link #isSupportSetStatusBar()}判断是否需要或者{@link #getStatusBarHeightWithSupportCheck()}
     */
    public static int getStatusBarHeight() {
        if (sStatusBarHeight == 0) {
            int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
            sStatusBarHeight = mContext.getResources().getDimensionPixelSize(resourceId);
        }
        return sStatusBarHeight;
    }

    /**
     * 获得状态栏高度, 如果系统版本不支持全屏, 会返回0
     */
    public static int getStatusBarHeightWithSupportCheck() {
        return isSupportSetStatusBar() ? getStatusBarHeight() : 0;
    }

    /**
     * 是否支持状态栏: 4.4以上支持全屏模式, 所以可以认为是支持
     */
    public static boolean isSupportSetStatusBar() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    /**
     * 设置状态栏占位视图, 颜色默认为app标题栏的颜色, 如果transparent为true则变成透明色
     */
    public static void setStatusBarViewPlaceHolder(View v, boolean transparent) {
        if (v == null || v.getLayoutParams() == null) {
            return;
        }
        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        layoutParams.height = getStatusBarHeight();
        v.setLayoutParams(layoutParams);
        if (transparent) {
            v.setBackgroundColor(Color.TRANSPARENT);
        }
    }

    /**
     * 状态栏文字调整为白色
     */
    public static void setStatusBarLightFontMode(Activity activity) {
        int op = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        setStatusBarLightFontMode(activity, op, Color.TRANSPARENT);
    }

    /**
     * 状态栏文字调整为白色
     */
    public static void setStatusBarLightFontMode(Activity activity, int color) {
        int op = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        setStatusBarLightFontMode(activity, op, color);
    }

    /**
     * 状态栏文字调整为白色
     *
     * @param activity
     * @param op
     */
    public static void setStatusBarLightFontMode(Activity activity, int op, int bgColor) {
        if (isSupportSetStatusBar()) {
            /* 先进行设置, 最后再根据系统类型来适配 */
            View view = activity.getWindow().getDecorView();
            view.setSystemUiVisibility(op);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                // 针对4.4系统, 增加透明状态栏模式, 系统不支持状态栏文字调色, 但是在这个模式下, 状态栏文字还是可以看清楚的
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                // 针对5.x系统, 直接设置透明状态栏, 因为当前状态栏文字只支持白色
                activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // 针对6.0+系统, 系统已经支持状态栏文字调色, 可以开启透明的模式, 切换成沉浸式UI了
                activity.getWindow().setStatusBarColor(bgColor);
            }
            fitStatusBarForMiuiOrMeizu(activity, false);
        }
    }

    /**
     * 状态栏文字调整为黑色
     */
    public static void setStatusBarDarkFontMode(Activity activity) {

        setStatusBarDarkFontMode(activity, Color.TRANSPARENT);
    }

    /**
     * 状态栏文字调整为黑色
     */
    public static void setStatusBarDarkFontMode(Activity activity, int color) {
        int op = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            op |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        }
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        setStatusBarDarkFontMode(activity, op, color);
    }

    /**
     * 状态栏文字调整为黑色
     *
     * @param activity
     * @param op
     */
    public static void setStatusBarDarkFontMode(Activity activity, int op, int color) {
        /* 先进行设置, 最后再根据系统类型来适配 */
        if (isSupportSetStatusBar()) {
            View view = activity.getWindow().getDecorView();
            view.setSystemUiVisibility(op);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                // 针对4.4系统, 增加透明状态栏模式, 系统不支持状态栏文字调色, 但是在这个模式下, 状态栏文字还是可以看清楚的
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                // 针对5.x系统, 增加半透明的状态栏背景, 因为5.x系统不支持状态栏文字调色
                activity.getWindow().setStatusBarColor(Color.parseColor("#4D000000"));
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // 针对6.0+系统, 系统已经支持状态栏文字调色, 可以开启透明的模式, 切换成沉浸式UI了
                activity.getWindow().setStatusBarColor(color);
            }
            fitStatusBarForMiuiOrMeizu(activity, true);
        }
    }

    /**
     * 适配魅族或者小米系统
     */
    @SuppressLint("NewApi")
    public static void fitStatusBarForMiuiOrMeizu(Activity activity, boolean dark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (XiaoMiStatusBarUtils.isMiuiDevice()) {
                // 6.0之上, 采用系统API, 6.0之下, 再采用Miui的Flag. 注意. 为了适配不同版本的MIUI, 两个都要调用
                // http://www.miui.com/thread-8946673-1-1.html
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    Window window = activity.getWindow();
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                }
                boolean result = XiaoMiStatusBarUtils.setMiuiStatusBarMode(activity.getWindow(), dark);
                // 针对5.x系统, 去除半透明的状态栏背景
                if (result && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
                }

            }
            if (MeiZuStatusBarUtils.isMeizuDevice()) {
                boolean result = MeiZuStatusBarUtils.setStatusBarDarkIcon(activity, dark);
                // 针对5.x系统, 去除半透明的状态栏背景
                if (result && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
                }
            }
        }
    }

    public static void setStatusBarPadding(View view) {
        if (isSupportSetStatusBar()) {
            view.setPadding(0, getStatusBarHeight(), 0, 0);
        }
    }

    public static void setStatusBarMargin(View view) {
        if (isSupportSetStatusBar()) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            layoutParams.topMargin = layoutParams.topMargin + getStatusBarHeight();
            view.setLayoutParams(layoutParams);
        }
    }
}
